define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classroom/lesson/index' + location.search,
                    add_url: 'classroom/lesson/add/course_id/' + Config.course_id,
                    edit_url: 'classroom/lesson/edit/course_id/' + Config.course_id,
                    del_url: 'classroom/lesson/del',
                    multi_url: 'classroom/lesson/multi',
                    import_url: 'classroom/lesson/import',
                    table: 'classroom_lesson',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3'),"4":__('Type 4')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'password', title: __('Password'),operate: false,
                            formatter: function (value, row, index) {
                                if(value !== ''){
                                    return `<span style="color: #f75444;">开启</span>`;
                                } else {
                                    return `<span style="color: #95a5a6;">无</span>`;
                                }
                            }
                        },
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {
                            field: 'time_up', title: __('Time_up'), searchList: {"1":__('Time_up 1'),"0":__('Time_up 0')},
                            formatter: function (value, row, index) {
                                if(value === 1){
                                    return `<span style="color: #1688f1;">${Table.api.formatter.datetime(row.up_time)}</span>`;
                                } else {
                                    return `<span style="color: #444c69;">关闭</span>`;
                                }
                            }
                        },
                        {field: 'up_time', title: __('Up_time'), visible:false,operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'course.name', title: __('Course.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {
                            field: 'countStudy', title: __('CountStudy'), operate: false,
                            formatter: function (value, row, index) {
                                return `<a href="classroom/course_record/index?ids=${row.course_id}&lesson_id=${row.id}" class="btn btn-xs btn-info btn-dialog" data-area="[&quot;95%&quot;,&quot;95%&quot;]" data-shade="[0.5,&quot;#000&quot;]" title="学习人数"><i class="fa fa-group"></i> ${value} 人</a>`;
                            }
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                // 章节类型
                $("input[type=radio][name='row[type]']").change(function () {
                    let v = this.value;
                    let file_type = $("input[type=radio][name='row[file_type]']:checked").val();
                    console.log('type',v);
                    console.log('file_type',file_type);
                    if(v === '4'){
                        $(".video_file_box").hide();
                        $(".audio_file_box").hide();
                        $(".content_box").hide();
                        $(".pdf_box").show();
                        $(".v_a_box").hide();
                        $(".time_box").hide();
                        $(".iframe_box").hide();
                    } else if(v === '3'){
                        $(".video_file_box").hide();
                        $(".audio_file_box").hide();
                        $(".content_box").show();
                        $(".pdf_box").hide();
                        $(".v_a_box").hide();
                        $(".time_box").hide();
                        $(".iframe_box").hide();
                    } else if(v === '2') {
                        $(".video_file_box").hide();
                        $(".audio_file_box").show();
                        $(".content_box").hide();
                        $(".pdf_box").hide();
                        $(".v_a_box").hide();
                        $(".time_box").show();
                        // if(file_type === '1'){
                        //     $(".video_file_box").hide();
                        //     $(".audio_file_box").show();
                        //     $(".iframe_box").hide();
                        // } else {
                        //     $(".iframe_box").show();
                        //     $(".video_file_box").hide();
                        //     $(".audio_file_box").hide();
                        // }
                    } else {
                        $(".content_box").hide();
                        $(".pdf_box").hide();
                        $(".v_a_box").show();
                        $(".time_box").show();
                        if(file_type === '1'){
                            $(".video_file_box").show();
                            $(".audio_file_box").hide();
                            $(".iframe_box").hide();
                        } else {
                            $(".iframe_box").show();
                            $(".video_file_box").hide();
                            $(".audio_file_box").hide();
                        }
                    }
                });
                // 存储类型
                $("input[type=radio][name='row[file_type]']").change(function () {
                    let v = this.value;
                    let type = $("input[type=radio][name='row[type]']:checked").val();
                    console.log(v);
                    if(v === '1'){
                        $(".iframe_box").hide();
                        if(type === '1'){
                            $(".video_file_box").show();
                            $(".audio_file_box").hide();
                        } else {
                            $(".audio_file_box").show();
                            $(".video_file_box").hide();
                        }
                    } else if(v === '2') {
                        $(".iframe_box").show();
                        $(".video_file_box").hide();
                        $(".audio_file_box").hide();
                    }
                });
                // 定时上架
                $("input[type=radio][name='row[time_up]']").change(function () {
                    let v = this.value;
                    if(v === '1'){
                        $(".up_time_box").show();
                    } else {
                        $(".up_time_box").hide();
                    }
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});